<?php
namespace app\api\model;

use app\admin\event\SystemLog;
use think\Model;
use think\Db;
class DisposeModel extends Base
{

    protected static function init()
    {
        self::observe(SystemLog::class);
    }

    public function getInformation($token)
    {
        $user =$this->getOne('token','',['token'=>$token]);
        if(!empty($user)){
            $list =$this->getOne('admin','username,name,phone,email,des',['id'=>$user['uid']]);
        }
        return $list;
    }

    public function getInformationEdit($id,$data)
    {
        $list =$this->getEdit('admin',['id'=>$id],$data);
        return $list;
    }

    public function getStaff($data)
    {
        $where =[];
        if(!empty($data['account'])){
            $where[] =['username','like','%'.$data['account'].'%'];
        }
        if(!empty($data['name'])){
            $where[] =['name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['status'])){
            $where[] =['status','=',$data['status']];
        }
        $list =$this->getPage('admin','id,name,store,end_time,ip,create_time,entry_time,status',$where,'id desc',$data['page'],$data['size']);
        $list =$list->toArray();
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $list['data'][$k]['store'] =$this->getValue('store',['id'=>$v['store']],'name');
            $list['data'][$k]['end_time'] =date('Y-m-d H:i:s',$v['end_time']);
            $list['data'][$k]['entry_time'] =date('Y-m-d H:i:s',$v['entry_time']);
        }
        return $list;
    }

    public function getStaffAdd($data)
    {
        $list =$this->getAdd('admin',$data);
        return $list;
    }

    public function getStaffInfo($id)
    {
        $list =$this->getOne('admin','*',['id'=>$id]);
        return $list;
    }

    public function getStaffEdit($id,$data)
    {
        $list =$this->getEdit('admin',['id'=>$id],$data);
        return $list;
    }

    public function getStaffDel($id)
    {
        $current =$this->getOne('admin','*',['id'=>$id]);
        $list1 =$this->getOne('instock','*',['maker'=>$id]);
        $list2 =$this->getOne('outstock','*',['maker'=>$id]);
        if($current['level'] ==1){
            return 1;
        }else if(!empty($list1) || !empty($list2)){
            return 2;
        }else{
            $this->getDel('admin',$id);
            return 3;
        }
    }

    public function getNotice($data)
    {
        $list =$this->getPage('notice','id,title,status,maker,create_time',[],'id desc',$data['page'],$data['size']);
        $list =$list->toArray();
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $list['data'][$k]['maker'] =$this->getValue('admin',['id'=>$v['maker']],'name');
        }
        return $list;
    }

    public function getNoticeInfo($id)
    {
        $list =$this->getOne('notice','id,title,content,images',['id'=>$id]);
        return $list;
    }

    public function getNoticeAdd($data)
    {
        $list =$this->getAdd('notice',$data);
        return $list;
    }

    public function getNoticeEdit($id,$data)
    {
        $list =$this->getEdit('notice',['id'=>$id],$data);
        return $list;
    }

    public function getNoticeDel($id)
    {
        $list =$this->getDel('notice',['id'=>$id]);
        return $list;
    }

    /*public function getLogistic($data)
    {
        $list =$this->getPage('','id,title,status,maker,create_time',[],'id desc',$data['limit'],$data['page']);
        //foreach()
        return $list;
    }*/

    public function getSys()
    {
        $list =$this->getOne('config','*',['id'=>1]);
        return $list;
    }

    public function getSysEdit($id,$data)
    {
        $list =$this->getEdit('config',['id'=>$id],$data);
        return $list;
    }

    public function getLog($data)
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['name'])){
            $where[] =['name','=',$data['name']];
        }
        $list =$this->getPage('log','*',$where,'id desc',$data['page'],$data['size']);
        $list =$list->toArray();
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $list['data'][$k]['maker'] =$this->getValue('admin',['id'=>$v['maker']],'username');
            $list['data'][$k]['name'] =$this->getValue('admin',['id'=>$v['maker']],'name');
        }
        return $list;
    }
}